<?php
declare(strict_types=1);
/**
 * 网关服务服务
 * Version :  v1.0.0
 * Date    :  2023/6/8 11:39
 */

namespace App\JsonRpc;

use function Hyperf\Support\env;
use Hyperf\RpcServer\Annotation\RpcService;
use Hyperf\Guzzle\ClientFactory;

// #[RpcService(name: "ServiceService", protocol: "jsonrpc-http", server: "jsonrpc-http", publishTo:"nacos")]
class ServiceService implements ServiceServiceInterface
{
    /**
     * @var ClientFactory HTTP客户端
     */
    protected ClientFactory $httpClient;

    /**
     * @var $gatewayManagerUrl string kong网关管理API地址
     */
    protected $gatewayManagerUrl;

    /**
     * @var $header array 公共请求头
     */
    protected $header;

    public function __construct(ClientFactory $clientFactory)
    {
        $this->httpClient = $clientFactory;
        $this->gatewayManagerUrl = env("GATEWAY_URL","http://localhost:8001");
        $this->header = [];
    }

    /**
     * 创建服务
     * date 2$data023/6/8 11:42
     * @param array $data 服务数据
     * @return array code 0:成功 1：失败， message 提示消息， data: 返回数据
     */
    public function createService(array $data): array
    {
        $client = $this->httpClient->create([]);
        try{
            $response = $client->request("POST", $this->gatewayManagerUrl."/services",["form_params"=>$data,"header"=>$this->header]);
            $result = json_decode($response->getBody()->getContents(),true);
            $httpCode = $response->getStatusCode();
            if($httpCode == 201){
                return ["code" => 0, "message" => "创建服务成功", "data" => $result];
            }
        }catch (\Throwable $e){
            var_dump("231",$e->getMessage());
            return ["code" => 1, "message" => $e->getMessage(), "data" => []];
        }
    }

    /**
     * 修改服务
     * date 2023/6/8 14:51
     * @param string $name 服务名称
     * @param array $data 服务数据
     * @return array code 0:成功 1：失败，message 提示消息，data: 返回数据
     */
    public function editService(string $name, array $data): array
    {
        $client = $this->httpClient->create([]);
        try{
            $response = $client->request("PATCH", $this->gatewayManagerUrl."/services/{$name}",["form_params"=>$data,"header"=>$this->header]);
            $result = json_decode($response->getBody()->getContents(),true);
            $httpCode = $response->getStatusCode();
            if($httpCode == 200){
                return ["code" => 0, "message" => "修改服务成功", "data" => $result];
            }
        }catch (\Throwable $e){
            return ["code" => 1, "message" => $e->getMessage(), "data" => []];
        }
    }

    /**
     * 获取服务详情
     * date 2023/6/8 14:51
     * @param string $name 服务名称
     * @return array code 0:成功 1：失败，message 提示消息，data: 返回数据
     */
    public function getService(string $name): array
    {
        $client = $this->httpClient->create([]);
        try{
            $response = $client->request("GET", $this->gatewayManagerUrl."/services/{$name}",["header"=>$this->header]);

            $result = json_decode($response->getBody()->getContents(),true);
            $httpCode = $response->getStatusCode();
            if($httpCode == 200){
                return ["code" => 0, "message" => "获取服务成功", "data" => $result];
            }else{
                return ["code" => 1, "message" => "服务不存在", "data" => []];
            }
        }catch (\Throwable $e){
            return ["code" => 1, "message" => $e->getMessage(), "data" => []];
        }
    }

    /**
     * 删除服务
     * date 2023/6/8 14:51
     * @param string $name 服务名称
     * @return array code 0:成功 1：失败，message 提示消息，data: 返回数据
     */
    public function deleteService(string $name): array
    {
        $client = $this->httpClient->create([]);
        try{
            $response = $client->request("DELETE", $this->gatewayManagerUrl."/services/{$name}",["header"=>$this->header]);

            $result = json_decode($response->getBody()->getContents(),true);
            $httpCode = $response->getStatusCode();

            if($httpCode == 204){
                return ["code" => 0, "message" => "删除服务成功", "data" => $result];
            }
        }catch (\Throwable $e){
            return ["code" => 1, "message" => $e->getMessage(), "data" => []];
        }
    }

}